#include "dlg_addstu.h"
#include "ui_dlg_addstu.h"
#include <QMessageBox>

Dlg_addStu::Dlg_addStu(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_addStu)
{
    ui->setupUi(this);
}

Dlg_addStu::~Dlg_addStu()
{
    delete ui;
}

void Dlg_addStu::setType(bool isAdd, StuInfo info)
{
    m_isAdd = isAdd;
    m_info = info;

    ui->le_name->setText(info.name);
    ui->sb_age->setValue(info.age);
    ui->le_class->setText(QString::number(info.uiclass));
    ui->le_grade->setText(QString::number(info.grade));
    ui->le_phone->setText(info.phone);
    ui->le_wechat->setText(info.wechat);
}

void Dlg_addStu::on_btn_cacel_clicked()
{
    this->hide();
}


void Dlg_addStu::on_btn_save_clicked()
{
    StuInfo info;
    auto ptr = stuSql::getinstance();

    info.name = ui->le_name->text();
    info.age = ui->sb_age->text().toInt();
    info.uiclass = ui->le_class->text().toInt();
    info.grade = ui->le_grade->text().toInt();
    info.phone = ui->le_phone->text();
    info.wechat = ui->le_wechat->text();
    info.stuid = ui->le_seq->text().toInt();


    if(m_isAdd)
    {
        ptr->addStu(info);
    }
    else
    {
        info.id = m_info.id;
        ptr->updateStuInfo(info);
    }

    QMessageBox::information(nullptr, "信息", "增加成功");
    this->hide();
}

